//
//  XJGoodsDetailBaseViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/11.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  这个类应该没用了
//self.selStandardsV

#import "XJGoodsDetailBaseViewController.h"
//header
#import "XJGoodsDetailHeader.h"//轮播
#import "XJGoodsDetailCommentHeader.h"//评论
//cell
#import "XJGoodsDetailTitleCollectionViewCell.h"//标题价格
#import "XJGoodsDetailFreightRuleCollectionViewCell.h"//配送规则
#import "XJGoodsDetailSpecificationsCollectionViewCell.h"//商品规格
#import "XJGoodsDetailCommentCollectionViewCell.h"//评论

//View
#import "XJSelectStandardsView.h" // 选择规格view
// Models
#import "XJFeatureSectionItemModel.h"


@interface XJGoodsDetailBaseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,XJGoodsDetailCommentHeaderDelegate>

@property (strong, nonatomic) UIScrollView *scrollerView;
@property (strong, nonatomic) UICollectionView *collectionView;

/* 通知 */
@property (weak ,nonatomic) id gdObj;

/**选择规格*/
@property(nonatomic, strong) XJSelectStandardsView *selStandardsV;

@end

//header
static NSString * GoodsDetailHeaderID = @"XJGoodsDetailHeader";
static NSString * GoodsDetailCommentHeaderID=@"XJGoodsDetailCommentHeader";
//cell
static NSString * GoodsDetailTitleCollectionViewCellID = @"XJGoodsDetailTitleCollectionViewCell";
static NSString * GoodsDetailFreightRuleCollectionViewCellID = @"XJGoodsDetailFreightRuleCollectionViewCell";
static NSString * GoodsDetailSpecificationsCollectionViewCellID = @"XJGoodsDetailSpecificationsCollectionViewCell";
static NSString * GoodsDetailCommentCollectionViewCellID = @"XJGoodsDetailCommentCollectionViewCell";

@implementation XJGoodsDetailBaseViewController

#pragma mark - lazy
- (XJSelectStandardsView *)selStandardsV {
    if (!_selStandardsV) {
        _selStandardsV = [XJSelectStandardsView new];
    }
    return _selStandardsV;
}

- (UIScrollView *)scrollerView
{
    if (!_scrollerView) {
        _scrollerView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _scrollerView.frame = CGRectMake(0, 0, KWidth, KHeight);
        _scrollerView.contentSize = CGSizeMake(KWidth, (KHeight - 50) * 2);
        _scrollerView.pagingEnabled = YES;
        _scrollerView.scrollEnabled = YES;
        [self.view addSubview:_scrollerView];
    }
    return _scrollerView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0; //Y
        layout.minimumInteritemSpacing = 0; //X
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.frame = CGRectMake(0, 0, KWidth, KHeight - 50);
 
        _collectionView.showsVerticalScrollIndicator = NO;
        [self.scrollerView addSubview:_collectionView];
        
        //注册header
        [_collectionView registerClass:[XJGoodsDetailHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:GoodsDetailHeaderID];
        [_collectionView registerClass:[XJGoodsDetailCommentHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:GoodsDetailCommentHeaderID];

        //注册Cell
        [_collectionView registerClass:[XJGoodsDetailTitleCollectionViewCell class] forCellWithReuseIdentifier:GoodsDetailTitleCollectionViewCellID];
        [_collectionView registerClass:[XJGoodsDetailFreightRuleCollectionViewCell class] forCellWithReuseIdentifier:GoodsDetailFreightRuleCollectionViewCellID];
        [_collectionView registerClass:[XJGoodsDetailSpecificationsCollectionViewCell class] forCellWithReuseIdentifier:GoodsDetailSpecificationsCollectionViewCellID];
        [_collectionView registerClass:[XJGoodsDetailCommentCollectionViewCell class] forCellWithReuseIdentifier:GoodsDetailCommentCollectionViewCellID];
        //注册Footer
 
        
    }
    return _collectionView;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = WHITE_COLOR;
    self.collectionView.backgroundColor = self.view.backgroundColor;
    self.scrollerView.backgroundColor = self.view.backgroundColor;
//    if ([UIDevice currentDevice].systemVersion.floatValue >= 7) {
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    }
//    XJGoodsDetailViewController * xjgdVC = (XJGoodsDetailViewController *)self.parentViewController;
//    XJLog(@"%@",self.parentViewController);
//    XJLog(@"%ld",(long)xjgdVC.type);
    [self acceptanceNote];

}

#pragma mark - 接受通知
- (void)acceptanceNote{
 
    
    
    //父类加入购物车，立即购买通知
    _gdObj = [[NSNotificationCenter defaultCenter]addObserverForName:SELECTCARTORBUY object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        if (YES) {
            if ([note.userInfo[@"buttonTag"] isEqualToString:@"0"]) { //加入购物车（父类）
                XJLog(@"加入购物车成功");
               // [weakSelf setUpWithAddSuccess];
                
                self.selStandardsV.dataArr = [self moniDataArr];
//                [self.selStandardsV rb_showSelectStandardsView];
                
            }else if ([note.userInfo[@"buttonTag"] isEqualToString:@"1"]){//立即购买（父类）
                XJLog(@"填写订单");
                
                self.selStandardsV.dataArr = [self moniDataArr];
//                [self.selStandardsV rb_showSelectStandardsView];
                
//                DCFillinOrderViewController *dcFillVc = [DCFillinOrderViewController new];
//                [weakSelf.navigationController pushViewController:dcFillVc animated:YES];
            }
            
        }else {
            XJLog(@"选择规格");
        }
    }];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

#pragma mark - <UICollectionViewDelegate>
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * xjgdCell = nil;
    if (indexPath.section == 0) {
        XJGoodsDetailTitleCollectionViewCell * xjgdTitleCell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsDetailTitleCollectionViewCellID forIndexPath:indexPath];
        xjgdCell = xjgdTitleCell;
    }else if (indexPath.section == 1){
        XJGoodsDetailFreightRuleCollectionViewCell * xjgdRuleCell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsDetailFreightRuleCollectionViewCellID forIndexPath:indexPath];
        xjgdCell = xjgdRuleCell;
    }else if (indexPath.section == 2){
        XJGoodsDetailSpecificationsCollectionViewCell * xjgdSpeCell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsDetailSpecificationsCollectionViewCellID forIndexPath:indexPath];
        xjgdCell = xjgdSpeCell;
    }else if (indexPath.section == 3){
        XJGoodsDetailCommentCollectionViewCell * xjgdCommentCell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsDetailCommentCollectionViewCellID forIndexPath:indexPath];
        xjgdCell = xjgdCommentCell;
    }
    
    return xjgdCell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader){
        if (indexPath.section==0) {
            XJGoodsDetailHeader * xjgd_header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:GoodsDetailHeaderID forIndexPath:indexPath];
            xjgd_header.shufflingArray = @[@"http://img.suneasecloud.com/uploadfilesapi/435/201801/22/201801220010.jpg",@"http://img.suneasecloud.com/uploadfilesapi/435/201801/22/201801220011.jpg",@"http://img.suneasecloud.com/uploadfilesapi/435/201801/22/201801220009.jpg",@"http://img.suneasecloud.com/uploadfilesapi/435/201801/22/201801220008.jpg"];
            return xjgd_header;
        }else if (indexPath.section==3){
            XJGoodsDetailCommentHeader * xjgd_cHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:GoodsDetailCommentHeaderID forIndexPath:indexPath];
            xjgd_cHeader.delegate = self;
            return xjgd_cHeader;
        }
        else{
            return nil;
        }
    }else{
        return nil;
    }
}

#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(KWidth, UI_IS_IPHONE5 ? ADAPTHeight(135) : ADAPTHeight(95));
    }else if (indexPath.section == 1){
        return CGSizeMake(KWidth, ADAPTHeight(50));
    }else if (indexPath.section == 2){
        return CGSizeMake(KWidth, ADAPTHeight(50));
    }else if (indexPath.section == 3){
        return CGSizeMake(KWidth, UI_IS_IPHONE5 ? ADAPTHeight(130) : ADAPTHeight(110));
    }else{
        return CGSizeZero;
    }
}


#pragma mark - head宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(KWidth, UI_IS_IPHONE5 ? ADAPTHeight(375) : ADAPTHeight(375));
    }else if (section == 3){
        return CGSizeMake(KWidth, ADAPTHeight(50));
    }
    else {
        return CGSizeZero;
    }
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        XJLog(@"商品名称、价格");
    }else if (indexPath.section == 1){
        XJLog(@"运费规则");
    }else if (indexPath.section == 2){
        XJLog(@"套餐、规格");
    }else if (indexPath.section == 3){
        XJLog(@"评价");
    }
}


#pragma mark - XJGoodsDetailCommentHeaderDelegate

- (void)moreViewAllCommentAction{
    XJLog(@"查看全部评价");
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:_gdObj];
}

#pragma mark - 模拟数据 显示选择规格view
// 模拟数据
- (NSMutableArray *)moniDataArr {
    
    NSArray *tempArr = @[ @{@"secTitle":@"机身颜色",
                            @"listArr":@[@{@"infoname":@"玫瑰金"},
                                         @{@"infoname":@"粉色"},
                                         @{@"infoname":@"灰"},
                                         @{@"infoname":@"磨砂黑"}
                                         ]
                            },
                          @{@"secTitle":@"网络类型",
                            @"listArr":@[@{@"infoname":@"全网通"},
                                         @{@"infoname":@"移动4G"},
                                         @{@"infoname":@"移动联通三网"},
                                         @{@"infoname":@"合约机"}
                                         ]
                            }
                          ];
    
    NSMutableArray *newArr = [NSMutableArray array];
    
    for (NSDictionary *dic in tempArr) {
        XJFeatureSectionItemModel *bigM = [XJFeatureSectionItemModel mj_objectWithKeyValues:dic];
        [newArr addObject:bigM];
    }
    
    return newArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
